<?php

namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class houtai extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    public function index()
	{
        
		return view('houtai/index');
	}
    public function getSystemInit(){
        $homeInfo = [
            'title' => '首页',
            'href'  => 'page/welcome-1.html?t=1',
        ];
        $logoInfo = [
            'title' => 'LAYUI MINI',
            'image' => '/static/images/logo.png',
        ];
        $menuInfo = $this->getMenuList();
        $systemInit = [
            'homeInfo' => $homeInfo,
            'logoInfo' => $logoInfo,
            'menuInfo' => $menuInfo,
        ];
        // dump($systemInit);
        return response()->json($systemInit);
    }

    // 获取菜单列表
    public function getMenuList(){
        $menuList = DB::table('url')
            ->select(['id','pid','title','icon','href','target'])
            // ->where('status', 1)
            // ->orderBy('sort', 'desc')
            ->get();
            $menuList = $this->buildMenuChild(0, $menuList);
            // dump($menuList);
        return $menuList;
    }

    //递归获取子菜单
    public function buildMenuChild($pid, $menuList){
        $treeList = [];
        foreach ($menuList as $v) {
            if ($pid == $v->pid) {
                $node = (array)$v;
                $child = $this->buildMenuChild($v->id, $menuList);
                if (!empty($child)) {
                    $node['child'] = $child;
                }
                // todo 后续此处加上用户的权限判断
                $treeList[] = $node;
            }
        }
        return $treeList;
    }

    public function table(){
        return view('houtai/table');
    }
    public function table_cha(Request $request){
        $input = $request->all();
        if(empty($input['searchParams'])){
            $search = '';
        }else{
            $search = json_decode($input['searchParams'],true);
            $search = $search['name'];
        }
        $table['data'] = Db::table('gie')->where("name","like","%{$search}%")->simplePaginate($input['limit'],'*',$input['page'],$input['page'])->items();
        
        $table['code'] = '0';
        $data = [
            "code"=>"0",
            "data"=>$table['data'],
            "count"=>Db::table('gie')->where("name","like","%{$search}%")->count()
        ];
        return  response()->json($data);
    }
    public function add(Request $request){
        $add_id = $request->all();
        if(!empty($add_id['id'])){
            $row = Db::table('gie')->where('id',$add_id['id'])->first();
            $row->hidden = $add_id['id'];
            // echo $row->add_html;
            return view('houtai/add',['row'=>$row]);
        }else{
            // dump(1);
            return view('houtai/add');
        }
        
        
    }
    public function add_ok(Request $request){
        $add_jie = $request->all();
        if(empty($add_jie['add_ok']['id'])){
            $add_ok = $add_jie['add_ok'];
            $add_ok['shuju_huixian'] = $add_ok['guige'];
            $add_ok['guige_huixian'] = $add_ok['kucun'];
            $add_ok['guige'] = json_decode($add_ok['guige'],true);
            $add_ok['kucun'] = json_decode($add_ok['kucun'],true);
            // $cha = Db::table('gie')->where('name',$add_ok['name'])->get();
            // dump($cha);
            // dump($add_jie);die();
            // dd($add_ok);
            $color = [];
            $color1 = [];
            
            // dump($add_ok);die();
            for($a=0;$a<count($add_ok['kucun']);$a++){
                // $color[$add_ok['kucun'][$a]['id']]['id'] = $add_ok['kucun'][$a]['name'];
                $bool = Db::table('guige')->where("guige","like",$add_ok['kucun'][$a]['name'])->first();
                // dump($add_ok['kucun'][$a]['name']);dump($bool);die();
                // die();
                if($bool == ''){
                    $add_guige['guige'] = $add_ok['kucun'][$a]['name'];
                    $boolssk = Db::table('guige')->insert($add_guige);
                    $bool = Db::table('guige')->where('guige',$add_guige['guige'])->first();
                    // dump($bool);
                }
                $bool = json_decode(json_encode($bool),true);
                $color[$add_ok['kucun'][$a]['id']]['id'] = $bool['id'];
                // dump($bool);
                // die();
                for($b=0;$b<count($add_ok['kucun'][$a]['sub']);$b++){
                    $color[$add_ok['kucun'][$a]['id']][$add_ok['kucun'][$a]['sub'][$b]['id']] = $add_ok['kucun'][$a]['sub'][$b]['name'];
                    $color1[$bool['id']][$add_ok['kucun'][$a]['sub'][$b]['id']] = $add_ok['kucun'][$a]['sub'][$b]['name'];
                }
            }
            $color3 = [];
            for($c=0;$c<count($add_ok['guige']);$c++){
                for($d=0;$d<count($add_ok['guige'][$c]['ids']);$d++){
                    $keys = array_keys($add_ok['guige'][$c]['ids'][$d]);
                    for($e=0;$e<count($keys);$e++){
                        $color3['class'][$keys[$e]] = $add_ok['guige'][$c]['ids'][$d][$keys[$e]];
                        $color4['class'][$color[$keys[$e]]['id']] = $color[$keys[$e]][$color3['class'][$keys[$e]]];
                    }
                }
                $add_ok['guige'][$c]['jiage'] = $add_ok['guige'][$c]['price'];
                $add_ok['guige'][$c]['kucun'] = $add_ok['guige'][$c]['stock'];
                unset($add_ok['guige'][$c]['stock']);
                unset($add_ok['guige'][$c]['price']);
                unset($add_ok['guige'][$c]['ids']);
                unset($add_ok['guige'][$c]['sku']);
                $add_ok['guige'][$c]['name1'] = $add_ok['name'];
                $add_ok['guige'][$c]['class'] = json_encode($color4['class'],JSON_UNESCAPED_UNICODE);
                $jiage[$c] = $add_ok['guige'][$c]['jiage'];
            }
            $add_ok['class'] = $color1;
            // dump($add_ok);die();
            $key_gui = array_keys($add_ok['class']);
            for($f=0;$f<count($key_gui);$f++){
                $key_ge[$f] = array_keys($add_ok['class'][$key_gui[$f]]);
                for($g=0;$g<count($key_ge[$f]);$g++){
                    $color5[$key_gui[$f]][$g] = $add_ok['class'][$key_gui[$f]][$key_ge[$f][$g]];
                }
                $color5[$key_gui[$f]] = implode(',',$color5[$key_gui[$f]]);
            }
            $add_ok['class'] = json_encode($color5,JSON_UNESCAPED_UNICODE);
            $add_ok2 = $add_ok['guige'];
            unset($add_ok['guige']);
            unset($add_ok['kucun']);
            $count = count($jiage);
            $biggest = $smallest = $jiage[$count - 1];
            #每次取出两个元素，比较两个元素的大小再与最大值和最小值比较
            for($i = 0; $i < $count - 1; $i += 2) {
                if($jiage[$i] > $jiage[$i + 1]) {
                    $bigger = $jiage[$i];
                    $smaller = $jiage[$i + 1];
                } else {
                    $bigger = $jiage[$i + 1];
                    $smaller = $jiage[$i];   
                }  
                if($bigger > $biggest) {
                    $biggest = $bigger;
                    }
                if($smaller < $smallest) {
                    $smallest = $smaller;
                    }
                }
                $add_ok['jiage'] = $smallest;
                $add_ok['selest'] = $add_ok['class'];
                $add_oks = [
                    'name' => $add_ok['name'],
                    'selest' => $add_ok['selest'],
                    'date' => date('Y-m-d h:i:s', time()),
                    'add_html' => $add_ok['add_html'],
                    'add2_html' => $add_ok['add2_html'],
                    'guige_huixian' => $add_ok['guige_huixian'],
                    'shuju_huixian' => $add_ok['shuju_huixian'],
                ];
                // dd($add_oks);
                $big_id = Db::table('gie')->insertGetId($add_oks);
                for($h=0;$h<count($add_ok2);$h++){
                    $add_ok2[$h]['fu'] = $big_id;
                }
                $bool_add = Db::table('kucun2')->insert($add_ok2);
            if($bool_add and $big_id!=''){
                return json_encode(['model'=>4,'think'=>'添加成功'],JSON_UNESCAPED_UNICODE);
                
            }else{
                return json_encode(['model'=>5,'think'=>'添加失败'],JSON_UNESCAPED_UNICODE);
            }
        }else{
            //修改
            $add_ok = $add_jie['add_ok'];
            $add_ok['shuju_huixian'] = $add_ok['guige'];
            $add_ok['guige_huixian'] = $add_ok['kucun'];
            $add_ok['guige'] = json_decode($add_ok['guige'],true);
            $add_ok['kucun'] = json_decode($add_ok['kucun'],true);
            // $cha = Db::table('gie')->where('name',$add_ok['name'])->get();
            // dump($cha);
            // dump($add_jie);die();
            // dd($add_ok);
            $color = [];
            $color1 = [];
            
            // dump($add_ok);die();
            for($a=0;$a<count($add_ok['kucun']);$a++){
                // $color[$add_ok['kucun'][$a]['id']]['id'] = $add_ok['kucun'][$a]['name'];
                $bool = Db::table('guige')->where("guige","like",$add_ok['kucun'][$a]['name'])->first();
                // dump($add_ok['kucun'][$a]['name']);dump($bool);die();
                // die();
                if($bool == ''){
                    $add_guige['guige'] = $add_ok['kucun'][$a]['name'];
                    $boolssk = Db::table('guige')->insert($add_guige);
                    $bool = Db::table('guige')->where('guige',$add_guige['guige'])->first();
                    // dump($bool);
                }
                $bool = json_decode(json_encode($bool),true);
                $color[$add_ok['kucun'][$a]['id']]['id'] = $bool['id'];
                // dump($bool);
                // die();
                for($b=0;$b<count($add_ok['kucun'][$a]['sub']);$b++){
                    $color[$add_ok['kucun'][$a]['id']][$add_ok['kucun'][$a]['sub'][$b]['id']] = $add_ok['kucun'][$a]['sub'][$b]['name'];
                    $color1[$bool['id']][$add_ok['kucun'][$a]['sub'][$b]['id']] = $add_ok['kucun'][$a]['sub'][$b]['name'];
                }
            }
            $color3 = [];
            for($c=0;$c<count($add_ok['guige']);$c++){
                for($d=0;$d<count($add_ok['guige'][$c]['ids']);$d++){
                    $keys = array_keys($add_ok['guige'][$c]['ids'][$d]);
                    for($e=0;$e<count($keys);$e++){
                        $color3['class'][$keys[$e]] = $add_ok['guige'][$c]['ids'][$d][$keys[$e]];
                        $color4['class'][$color[$keys[$e]]['id']] = $color[$keys[$e]][$color3['class'][$keys[$e]]];
                    }
                }
                $add_ok['guige'][$c]['jiage'] = $add_ok['guige'][$c]['price'];
                $add_ok['guige'][$c]['kucun'] = $add_ok['guige'][$c]['stock'];
                unset($add_ok['guige'][$c]['stock']);
                unset($add_ok['guige'][$c]['price']);
                unset($add_ok['guige'][$c]['ids']);
                unset($add_ok['guige'][$c]['sku']);
                $add_ok['guige'][$c]['name1'] = $add_ok['name'];
                $add_ok['guige'][$c]['class'] = json_encode($color4['class'],JSON_UNESCAPED_UNICODE);
                $jiage[$c] = $add_ok['guige'][$c]['jiage'];
            }
            $add_ok['class'] = $color1;
            // dump($add_ok);die();
            $key_gui = array_keys($add_ok['class']);
            for($f=0;$f<count($key_gui);$f++){
                $key_ge[$f] = array_keys($add_ok['class'][$key_gui[$f]]);
                for($g=0;$g<count($key_ge[$f]);$g++){
                    $color5[$key_gui[$f]][$g] = $add_ok['class'][$key_gui[$f]][$key_ge[$f][$g]];
                }
                $color5[$key_gui[$f]] = implode(',',$color5[$key_gui[$f]]);
            }
            $add_ok['class'] = json_encode($color5,JSON_UNESCAPED_UNICODE);
            $add_ok2 = $add_ok['guige'];
            unset($add_ok['guige']);
            unset($add_ok['kucun']);
            $count = count($jiage);
            $biggest = $smallest = $jiage[$count - 1];
            #每次取出两个元素，比较两个元素的大小再与最大值和最小值比较
            for($i = 0; $i < $count - 1; $i += 2) {
                if($jiage[$i] > $jiage[$i + 1]) {
                    $bigger = $jiage[$i];
                    $smaller = $jiage[$i + 1];
                } else {
                    $bigger = $jiage[$i + 1];
                    $smaller = $jiage[$i];   
                }  
                if($bigger > $biggest) {
                    $biggest = $bigger;
                    }
                if($smaller < $smallest) {
                    $smallest = $smaller;
                    }
                }
                
                $add_ok['jiage'] = $smallest;
                $add_ok['selest'] = $add_ok['class'];
                $add_oks = [
                    'name' => $add_ok['name'],
                    'selest' => $add_ok['selest'],
                    'date' => date('Y-m-d h:i:s', time()),
                    'add_html' => $add_ok['add_html'],
                    'add2_html' => $add_ok['add2_html'],
                    'guige_huixian' => $add_ok['guige_huixian'],
                    'shuju_huixian' => $add_ok['shuju_huixian'],
                ];
                dd($add_oks);
                $big_id = Db::table('gie')->insertGetId($add_oks);
                for($h=0;$h<count($add_ok2);$h++){
                    $add_ok2[$h]['fu'] = $big_id;
                }
                $bool_add = Db::table('kucun2')->insert($add_ok2);
            if($bool_add and $big_id!=''){
                return json_encode(['model'=>4,'think'=>'添加成功'],JSON_UNESCAPED_UNICODE);
                
            }else{
                return json_encode(['model'=>5,'think'=>'添加失败'],JSON_UNESCAPED_UNICODE);
            }

        }
    }
    public function delete(Request $request){
        $delete = $request->all();
        $id = $delete['delete_id'];
        $res=Db::table('gie')->delete($id);
        if($res){
            return json_encode(['delete_bool'=>'1','zhuangtai'=>'删除成功'],JSON_UNESCAPED_UNICODE);
        }else{
            return json_encode(['delete_bool'=>'0','zhuangtai'=>'删除失败'],JSON_UNESCAPED_UNICODE);
        }
        // dd($delete);
    }
    public function delete_max(Request $request)
    {   
        $quan = $request->all();
        $quan = $quan['data'];
        $quan_shu = count($quan);
        for($a=0;$a<$quan_shu;$a++){
            $dele[$a] = $quan[$a]['id'];
        }
        // dd($dele);
        // dump($dele);
        // die();
        // $dele = $_GET['check'] ?? '';
        // @$dele_number = count($dele);
        if($dele==''){
            return json(['delete_bool'=>'0','zhuangtai'=>'请至少勾选一个数据']);
        }else if($dele!=''){
            // dd($dele);
                $res=Db::table('gie')->whereIn('id',$dele)->delete();
                
            if($res){
                return json_encode(['delete_bool'=>'1','zhuangtai'=>'删除成功'],JSON_UNESCAPED_UNICODE);
            }else{
                return json_encode(['delete_bool'=>'0','zhuangtai'=>'删除失败'],JSON_UNESCAPED_UNICODE);
            }
        }
    }
    public function table_user(){
        return view('houtai/table_user');
    }

    public function table_user_cha(Request $request){
        $ssks = $request->all();
        $table['code'] = '0';
        if(empty($ssks['searchParams'])){
            $search = '';
        }else{
            $search = json_decode($ssks['searchParams'],true);
            $search = $search['username'];
        }
        // dd($search);
        $table['data'] = Db::table('user')->where("username","like","%{$search}%")->simplePaginate($ssks['limit'],'*',$ssks['page'],$ssks['page'])->items();
        $table['count'] = Db::table('user')->where("username","like","%{$search}%")->count();
        // if($table['data']){
        
        // }
        // $table=0;
        // dd($table);
        return response()->json($table);
        
    }
    public function add_user(Request $request)
    {
        $row['username'] = '';
        $row['pwd'] = '';
        $row['tel'] = '';
        $row['hidden'] = 'add';
        $id=$request->all();
        // $id=$id->id??'';
        // $s=(Db::table('dindan')->where('id',34)->first())->xiadan_guige;
        // $s1 = (Db::table('kucun2')->where('id',$s)->first())->class;
        // $s1 = json_decode($s1,true);
        // $keys = array_keys($s1);
        // for($a=0;$a<count($keys);$a++){
        //     $zhi[$a]['id'] = (Db::table('guige')->where('id',$keys[$a])->first())->guige;
        //     $zhi[$a]['zhi'] = $s1[$keys[$a]];
        // }
        // $sssk = Db::table('guige')->get();
        // $sssk = json_decode(json_encode($sssk), true);
        // dump($sssk);
        // dd($zhi);
        $id=$id['id']??'';
        if(!empty($id)){
            // dump($id);
            $row=Db::table('user')->where("id",$id)->first();
            $row = json_decode(json_encode($row), true);
            $row['hidden'] = $id;
        }
        // dump($row);
        return view('houtai/add_user',['row'=>$row]);
    }

    public function add_user_ok(Request $request){
        $date=$request->all();
        // dd($id);
        if($date['add_user_ok']['password'] != $date['add_user_ok']['repassword']){
            return json(['model'=>2,'think'=>'密码不一致']);
        }else if($date['add_user_ok']['hidden']=='add'){
            $row['name'] = $date['add_user_ok']['name'];
            $row['username'] = $date['add_user_ok']['username'];
            $row['password'] = $date['add_user_ok']['password'];
            $row['tel'] = $date['add_user_ok']['tel'];
            
            $res=Db::table('user')->insert($row);
            if($res){
                return json_encode(['model'=>4,'think'=>'添加成功'],JSON_UNESCAPED_UNICODE);
            }else{
                return json_encode(['model'=>5,'think'=>'添加失败'],JSON_UNESCAPED_UNICODE);
            }
        }else{
            $row['name'] = $date['add_user_ok']['name'];
            $row['username'] = $date['add_user_ok']['username'];
            $row['password'] = $date['add_user_ok']['password'];
            $row['tel'] = $date['add_user_ok']['tel'];
            // dd($row);
            $res=Db::table('user')->where('id',$date['add_user_ok']['hidden'])->update($row);
            if($res){
                return json_encode(['model'=>6,'think'=>'修改成功'],JSON_UNESCAPED_UNICODE);
            }else{
                return json_encode(['model'=>5,'think'=>'修改失败'],JSON_UNESCAPED_UNICODE);
            }
        }
    }
}



